namespace Core {
    import Handler = Laya.Handler;

    class ResourceItem {
        public name: string
        public type: string
        public url: string
        public loadType: string
    }
    class ResourceGroup {
        public name: string
        public keys: string
        public res: string[]
    }

    class ResourceDescription {
        public groups: ResourceGroup[]
        public resources: ResourceItem[]
    }

    export class ResourceUtils extends BaseClass {
        private _resNameUrlMap: Object
        private _resConfig: Object
        private _allResourceItems: Object
        private _allResourceGroup: Object

        constructor() {
            super()
            this._resNameUrlMap = {}
            this._resConfig = {}
            this._allResourceItems = {}
            this._allResourceGroup = {}

            Laya.loader.on(Laya.Event.ERROR, this, this._onLoadError)
        }


        public loadConfig(urls: string[], onComplete: Function, caller: any): void {
            // var url: string = "res/" + fileName
            for (var i = 0; i < urls.length; i++) {
                this._resNameUrlMap[urls[i]] = "res/" + urls[i]
                urls[i] = "res/" + urls[i]
            }
            Laya.loader.load(urls, Handler.create(this, this._onLoadConfig, [Handler.create(caller, onComplete), urls]), null, Laya.Loader.TEXT)
        }

        public loadGroup(groups: string[], onComplete: Function, onProgress: Function, caller: any): void {
            //["../../res/apes/monkey0.png", "../../res/apes/monkey1.png", "../../res/apes/monkey2.png"]
            var urls: any[] = []
            for (var g = 0; g < groups.length; g++) {
                var group = <ResourceGroup>this._allResourceGroup[groups[g]]
                if (group) {
                    for (var i = 0; i < group.res.length; i++) {
                        var resItem = <ResourceItem>this._allResourceItems[group.res[i]]
                        if (resItem) {
                            urls.push({ url: resItem.url, type: resItem.loadType })
                        }
                    }
                    //console.log(group.name)
                }
            }
            if (urls.length > 0) {
                Laya.loader.load(urls, Handler.create(caller, onComplete), Handler.create(caller, onProgress))
            }
        }

        public loadResource(resources: string[], onComplete: Function, onProgress: Function, caller: any): void {
            Laya.loader.load(resources, Handler.create(caller, onComplete), Handler.create(caller, onProgress))
        }

        public getRes(resName: string): any {
            var url = <ResourceItem>this._allResourceItems[resName]
            if (url) {
                return Laya.loader.getRes(url.url)
            }
            return null
        }

        private _getLoadType(name: string): string {
            switch (name) {
                case "text": return Laya.Loader.TEXT
                case "atlas": return Laya.Loader.ATLAS
                case "image": return Laya.Loader.IMAGE
                case "bin": return Laya.Loader.BUFFER
                case "font": return Laya.Loader.FONT
                case "sound": return Laya.Loader.SOUND
                case "json": return Laya.Loader.JSON
            }
            return ""
        }

        private _onLoadConfig(handler: laya.utils.Handler, urls: string[], sucess: boolean) {
            //console.log(handler)
            //console.log(url)
            //console.log(sucess)
            if (sucess == true) {
                for (var i = 0; i < urls.length; i++) {
                    var name: string = urls[i]
                    var data = <ResourceDescription>JSON.parse(<string>Laya.loader.getRes(name))
                    for (var ig = 0; ig < data.groups.length; ig++) {
                        var group = <ResourceGroup>data.groups[i]
                        group.res = group.keys.split(",", -1)
                        this._allResourceGroup[group.name] = group
                    }
                    for (var ir = 0; ir < data.resources.length; ir++) {
                        var res = <ResourceItem>data.resources[ir]
                        res.loadType = this._getLoadType(res.type)
                        this._allResourceItems[data.resources[ir].name] = res
                    }
                    console.log(data)
                }
                if (handler.caller && handler.method) {
                    handler.method.apply(handler.caller, urls)
                }
            }
        }

        private _onLoadError(res: string): void {
            console.log("Error: " + res)
        }

    }
}